<?php/** * * @author wangtao * @version  */
class WT_FEED
{
	const APP = 'app';
	const ACTION = 'action';
	public static $param = '';

	/**
	 * 构造函数
	 * @param $module
	 */
	function __construct($get)
	{
		if (empty($get[0]) || $get[0] == 'index.html') {
			#默认参数
			$application = DEFAULT_APP;
		}
		else {
			$application = $get[0];
			unset($get[0]);
		}
		#载入应用文件
		$appFile = WT_ROOT . 'apps/' . $application . '.php';
		if (!file_exists($appFile)) {
			globals::E404();
		}


		include $appFile;		#查看方法
		if (isset($get[1]) && $get[1]) {
			$method = $get[1];
			unset($get[1]);
			$method = self::checkMethod($application, $method);
		}
		else {
			self::$param = array();
			$method = DEFAULT_METHOD;
			$method = self::checkMethod($application, $method);
		}
		if (isset($get[2]) && $get[2]) {
			self::$param = self::getParam($get);
		}
		else {
			self::$param = array();
		}
		$param = self::$param;
		$app = new $application;
		call_user_func_array(array($application, $method), $param);
	}

	/**
	 * 获取参数
	 */
	private static function getParam($get)
	{
		if(isset($_GET['jsonp'])) {
			unset($_GET['jsonp']);
		}
		if(isset($_GET['timestamp'])) {
			unset($_GET['timestamp']);
		}
		$params = array_values($get);
		return $params;
	}

	/**
	 * 获取类中对应的方法
	 * @param $class
	 * @param $method
	 */
	private static function checkMethod($class, $method)
	{
		#$method = strtolower($method);
		$methods = get_class_methods($class);
		if (in_array($method, $methods)) {
			return $method;
		}
		else {
			globals::E404();
		}
	}
}
